package com.vision.shop.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.vision.shop.model.Pager;
import com.vision.shop.model.SystemContext;
import com.vision.shop.util.DaoUtil;
import com.vision.shop.util.MyBatisUtil;

public class BaseDao<T> {
	public BaseDao(){
		DaoUtil.diObj(this);
	}
	
	public void add(T obj){
		SqlSession session=null;
		try {
			session=MyBatisUtil.createSession();
			session.insert(obj.getClass().getName()+".add",obj);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.rollback();
		}finally{
			MyBatisUtil.closeSession(session);
		}
		
	}
	public void add(String sqlId,Object obj){
		SqlSession session=null;
		try {
			session=MyBatisUtil.createSession();
			session.insert(sqlId,obj);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.rollback();
		}finally{
			MyBatisUtil.closeSession(session);
		}
		
	}
	public void update(T obj){
		SqlSession session=null;
		try {
			session=MyBatisUtil.createSession();
			session.update(obj.getClass().getName()+".update", obj);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.rollback();
		}finally{
			MyBatisUtil.closeSession(session);
		}
	}
	public void delete(Class<T> cls,int id){
		SqlSession session=null;
		try {
			session=MyBatisUtil.createSession();
			session.delete(cls.getName()+".delete",id);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.rollback();
		}finally{
			MyBatisUtil.closeSession(session);
		}
	}
	public void delete(String sqlId,Map<String,Object> params){
		SqlSession session=null;
		try {
			session=MyBatisUtil.createSession();
			session.delete(sqlId,params);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.rollback();
		}finally{
			MyBatisUtil.closeSession(session);
		}
	}
	@SuppressWarnings("unchecked")
	public T load(Class<T> cls,int id){
		SqlSession session=null;
		T t=null;
		try {
			session=MyBatisUtil.createSession();
			t=(T)session.selectOne(cls.getName()+".load", id);
		}finally{
			MyBatisUtil.closeSession(session);
		}
		return t;
	}
	@SuppressWarnings("unchecked")
	public T loadBySqlId(String sqlId,Map<String,Object> params){
		SqlSession session=null;
		T t=null;
		try {
			session=MyBatisUtil.createSession();
			t=(T)session.selectOne(sqlId,params);
		}finally{
			MyBatisUtil.closeSession(session);
		}
		return t;
	}
	@SuppressWarnings("unchecked")
	public T loadBySqlId(String sqlId,Object obj){
		SqlSession session=null;
		T t=null;
		try {
			session=MyBatisUtil.createSession();
			t=(T)session.selectOne(sqlId,obj);
		}finally{
			MyBatisUtil.closeSession(session);
		}
		return t;
	}
	
	public Pager<T> find(Class<T> cls,Map<String,Object> params){
		return this.find(cls.getName()+".find", params);
	}
	public Pager<T> find(String sqlId,Map<String,Object> params){
		int pageSize=SystemContext.getPageSize();
		int pageOffset=SystemContext.getPageOffset();
		String order=SystemContext.getOrder();
		String sort=SystemContext.getSort();
		Pager<T> pages=new Pager<T>();
		SqlSession session=null;
		try {
			session=MyBatisUtil.createSession();
			if(params==null){
				params=new HashMap<String,Object>();
			}
			params.put("pageSize", pageSize);
			params.put("pageOffset", pageOffset);
			params.put("order", order);
			params.put("sort", sort);
			List<T> datas=session.selectList(sqlId,params);
			pages.setDatas(datas);
			pages.setPageOffset(pageOffset);
			pages.setPageSize(pageSize);
			int totalRecord=session.selectOne(sqlId+"_count",params);
			pages.setTotalRecord(totalRecord);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			MyBatisUtil.closeSession(session);
		}
		return pages;
	}
	//不带分页
	public List<T> list(Class<T> cls,Map<String,Object> params){
		return this.list(cls.getName()+".list", params);
	}
	public List<T> list(String sqlId,Map<String,Object> params){
		List<T> list=null;
		SqlSession session=null;
		try {
			session=MyBatisUtil.createSession();
			list=session.selectList(sqlId,params);
			
		} finally{
			MyBatisUtil.closeSession(session);
		}
		return list;
	}
}
